package com.tsmti.tags.grid;

import com.tsmti.core.common.constant.Constants;
import com.tsmti.core.util.MessageUtils;
import com.tsmti.core.util.StringUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.Tag;
import java.util.HashMap;
import java.util.Map;


/**
 * 
 * @title: DataGridToolbarTag.java
 * @package
 * @description:工具栏目
 * @author:
 * @date: 2017年3月12日 下午1:32:07
 * @version
 * @copyright: 2017
 *
 */
@SuppressWarnings("serial")
public class DataGridToolbarTag extends AbstractGridHtmlTag {
	private static String[] INNER_DEFAULT_FUNCTION = { "create", "update", "delete", "search", "reset" };
	/**标题文字*/
	private String title = "";
	/**打开窗口宽度*/
	private String winwidth = "800px";
	/**打开窗口高度*/
	private String winheight = "500px";
	/**按样式*/
	private String btnclass = "";
	/**图标*/
	private String icon = "";
	/**方法请求地址*/
	private String url = "";
	/**默认的create,update,delete方法*/
	private String function = "";
	/**click事件*/
	private String onclick = "";
	/**onkeypress事件*/
	private String onkeypress = "";
	/**onfocus*/
	private String onfocus = "";
	/**left，right;默认在左边*/
	private String layout = "left";
	/**当时询问时打开的时候的询问提示语*/
	private String tipMsg = "";
	/**表达式*/
	private String exp = "";
	/**工具栏模式*/
	private String toolbarMode;
	/**名称*/
	private String name;
	/***  绑定数据字典类型，数据字典优先*/
	private String dict = "";
	/***  工具栏输入框 提示语*/
	private String placeholder = "";
	/***  工具栏输入框 高度*/
	private String height = "";
	/***  工具栏输入框 宽度*/
	private String width = "";
	/***  工具栏输入框 默认值*/
	private String value = "";

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getFunction() {
		return function;
	}

	public void setFunction(String function) {
		this.function = function;
	}

	public String getOnclick() {
		return onclick;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	public String getOnkeypress() {
		return onkeypress;
	}

	public void setOnkeypress(String onkeypress) {
		this.onkeypress = onkeypress;
	}

	public String getLayout() {
		return layout;
	}

	public void setLayout(String layout) {
		this.layout = layout;
	}

	public String getWinwidth() {
		return winwidth;
	}

	public void setWinwidth(String winwidth) {
		this.winwidth = winwidth;
	}

	public String getWinheight() {
		return winheight;
	}

	public void setWinheight(String winheight) {
		this.winheight = winheight;
	}

	public String getTipMsg() {
		return tipMsg;
	}

	public void setTipMsg(String tipMsg) {
		this.tipMsg = tipMsg;
	}

	public String getExp() {
		return exp;
	}

	public void setExp(String exp) {
		this.exp = exp;
	}
	

	public String getBtnclass() {
		return btnclass;
	}

	public void setBtnclass(String btnclass) {
		this.btnclass = btnclass;
	}

	@Override
	public int doEndTag() throws JspTagException {
		// toobar参数配置
		Tag t = findAncestorWithClass(this, DataGridTag.class);
		final DataGridTag parent = (DataGridTag) t;
		dealDefault(parent);
		Map<String, Object> toobarMap = new HashMap<String, Object>(16);
		toobarMap.putAll(staticAttributes);
		if (dynamicAttributes == null) {
			dynamicAttributes = new HashMap<String, Object>(16);
		}
		toobarMap.put("dynamicAttributes", dynamicAttributes);
		if (!StringUtils.isEmpty(dict)) {
			parent.putColumnDict(dict);
		}
		parent.addToolbar(toobarMap);
		return EVAL_PAGE;
	}

	private void dealDefault(DataGridTag parent) {
		if (!StringUtils.isEmpty(this.function) && isFunction(this.function)) {
			// 预处理Url问题
			if (StringUtils.isEmpty(url)) {
				String url = "";
				if (Constants.FUNCTION_DELETE.equals(this.function)) {
					url = parent.getBaseUrl() + "/batch/delete";
				} else if (Constants.FUNCTION_UPDATE.equals(this.function)) {
					url = parent.getBaseUrl() + "/{id}/" + this.function;
				} else {
					url = parent.getBaseUrl() + "/" + this.function;
				}
				staticAttributes.put("url", url);
			}

			if (StringUtils.isEmpty(title)) {
				String title = "sys.common." + this.function;
				staticAttributes.put("title", MessageUtils.getMessageOrSelf(title));
			}

			if (StringUtils.isEmpty(this.icon)) {
				String icon = "";
				if (Constants.FUNCTION_CREATE.equals(this.function)) {
					// btn-info
					icon = "icon-add";
				} else if (Constants.FUNCTION_UPDATE.equals(this.function)) {
					icon = "icon-edit";
				} else if (Constants.FUNCTION_DELETE.equals(this.function)) {
					icon = "icon-remove";
				} else if (Constants.FUNCTION_SERARCH.equals(this.function)) {
					icon = "icon-search";
				} else if (Constants.FUNCTION_RESET.equals(this.function)) {
					icon = "icon-refresh";
				}
				staticAttributes.put("icon", icon);
			}

			if (StringUtils.isEmpty(this.btnclass)) {
				String btnclass = "";
//				if (Constants.FUNCTION_CREATE.equals(this.function)) {
//					// btn-info
//					btnclass = "btn-primary";
//				} else if (Constants.FUNCTION_UPDATE.equals(this.function)) {
//					btnclass = "btn-success";
//				} else if (Constants.FUNCTION_DELETE.equals(this.function)) {
//					btnclass = "btn-danger";
//				} else if (Constants.FUNCTION_SERARCH.equals(this.function)) {
//					btnclass = "btn-info";
//				} else if (Constants.FUNCTION_RESET.equals(this.function)) {
//					btnclass = "btn-warning";
//				} else {
//					btnclass = "btn-info";
//				}
				staticAttributes.put("btnclass", btnclass);
			}

			if (Constants.FUNCTION_SERARCH.equals(this.function) || Constants.FUNCTION_RESET.equals(this.function)) {
				staticAttributes.put("layout", "right");
			}
		}
	}


	@Override
	public void setDynamicAttribute(String url, String localName, Object value) throws JspException {
		super.setDynamicAttribute(url, localName, value);
		if (Constants.LOCALNAME_TITLE.equals(localName) && StringUtils.isEmpty(title)) {
			dynamicAttributes.put(localName, MessageUtils.getMessageOrSelf((String) value));
		}
	}

	@Override
	public void setStaticAttribute(String localName, Object value) throws JspException {
		super.setStaticAttribute(localName, value);
		if (Constants.LOCALNAME_TITLE.equals(localName)) {
			staticAttributes.put(localName, MessageUtils.getMessageOrSelf((String) value));
		}
	}

	public Boolean isFunction(String function) {
		for (String defaultFunction : INNER_DEFAULT_FUNCTION) {
			if (defaultFunction.equals(function.toLowerCase())) {
				return Boolean.TRUE;
			}
		}
		return Boolean.FALSE;
	}

	public String getToolbarMode() {
		return toolbarMode;
	}

	public void setToolbarMode(String toolbarMode) {
		this.toolbarMode = toolbarMode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDict() {
		return dict;
	}

	public void setDict(String dict) {
		this.dict = dict;
	}

	public String getPlaceholder() {
		return placeholder;
	}

	public void setPlaceholder(String placeholder) {
		this.placeholder = placeholder;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getOnfocus() {
		return onfocus;
	}

	public void setOnfocus(String onfocus) {
		this.onfocus = onfocus;
	}
}